<?php
namespace app\api\controller;
use app\BaseController;
use app\common\model\StaffModel;

use app\common\model\StaffManageModel;

use app\common\model\UserNotesModel;

use app\common\model\RecordModel;
use app\common\model\VisitorModel;

class ManageData extends BaseController{
    public function data(){
        $manage_code = $this->request->param('manage_code/d',0);
        if($manage_code == 0){
            return error('参数错误');
        }
        $staff_id = StaffModel::where('code',$manage_code)->value('staff_id');
        $staff_manage_id = StaffManageModel::where([
            'user_id'=>$this->request->user_info->user_id,
            'staff_id' => $staff_id
            ])->value('staff_manage_id');
        if(!$staff_manage_id){
            return error('无权限');
        }
        //访问量
        $record = [];
        $record['total'] = RecordModel::where('staff_id',$staff_id)->count() ?? 0;
        $record['today'] = RecordModel::where('staff_id',$staff_id)->whereDay('create_time')->count() ?? 0;
        $record['month'] = RecordModel::where('staff_id',$staff_id)->whereMonth('create_time')->count() ?? 0;
        //访客量
        $visitor = [];
        $visitor['total'] = VisitorModel::where('staff_id',$staff_id)->count() ?? 0;
        $visitor['today'] = VisitorModel::where('staff_id',$staff_id)->whereDay('create_time')->count() ?? 0;
        $visitor['month'] = VisitorModel::where('staff_id',$staff_id)->whereMonth('create_time')->count() ?? 0;
        return success([
            'record'=>$record,
            'visitor'=>$visitor
        ]);
    }
    public function list(){
        $manage_code = $this->request->param('manage_code/d',0);
        if($manage_code == 0){
            return error('参数错误');
        }
        $staff_id = StaffModel::where('code',$manage_code)->value('staff_id');
        $staff_manage_id = StaffManageModel::where([
            'user_id'=>$this->request->user_info->user_id,
            'staff_id' => $staff_id
            ])->value('staff_manage_id');
        if(!$staff_manage_id){
            return error('无权限');
        }
        $type = $this->request->param('type/s','day');
        if($type == 'day'){
            $list = RecordModel::with(['user'])
            ->field('user_id,staff_id,duration,info,create_time,count(record_id) as count')
            ->where('staff_id',$staff_id)
            ->group('user_id')
            ->whereDay('create_time')
            ->limit($this->request->limit)
            ->page($this->request->page)
            ->order('record_id','desc')
            ->select()->append(['user_notes']);
        }
        
        if($type == 'yesterday'){
            $list = RecordModel::with(['user'])
            ->field('user_id,staff_id,duration,info,create_time,count(record_id) as count')
            ->where('staff_id',$staff_id)
            ->group('user_id')
            ->whereDay('create_time', 'yesterday')
            ->limit($this->request->limit)
            ->page($this->request->page)
            ->order('record_id','desc')
            ->select()->append(['user_notes']);
        }
        
        if($type == 'week'){
            $list = RecordModel::with(['user'])
            ->field('user_id,staff_id,duration,info,create_time,count(record_id) as count')
            ->where('staff_id',$staff_id)
            ->group('user_id')
            ->whereWeek('create_time')
            ->limit($this->request->limit)
            ->page($this->request->page)
            ->order('record_id','desc')
            ->select()->append(['user_notes']);
        }
        
        if($type == 'month'){
            $list = RecordModel::with(['user'])
            ->field('user_id,staff_id,duration,info,create_time,count(record_id) as count')
            ->where('staff_id',$staff_id)
            ->group('user_id')
            ->whereMonth('create_time')
            ->limit($this->request->limit)
            ->page($this->request->page)
            ->order('record_id','desc')
            ->select()->append(['user_notes']);
        }
        
        if($type == 'year'){
            $list = RecordModel::with(['user'])
            ->field('user_id,staff_id,duration,info,create_time,count(record_id) as count')
            ->where('staff_id',$staff_id)
            ->group('user_id')
            ->whereYear('create_time')
            ->limit($this->request->limit)
            ->page($this->request->page)
            ->order('record_id','desc')
            ->select()->append(['user_notes']);
        }
        
        return success(['list'=>$list]);
    }
    
    public function notesSave(){
        $manage_code = $this->request->param('manage_code/d',0);
        if($manage_code == 0){
            return error('参数错误');
        }
        $user_notes = $this->request->param('user_notes/s','');
        $staff_id = StaffModel::where('code',$manage_code)->value('staff_id');
        
        $user_id = $this->request->param('user_id/d',0);
        
        $staff_manage_id = StaffManageModel::where([
            'user_id'=>$this->request->user_info->user_id,
            'staff_id' => $staff_id
            ])->value('staff_manage_id');
        if(!$staff_manage_id){
            return error('无权限');
        }
        
        $UserNotesModel = UserNotesModel::where([
            'user_id' => $user_id,
            'staff_id' => $staff_id
            ])->find();
        
        if($UserNotesModel == null){
            $UserNotesModel = new UserNotesModel();
            $UserNotesModel->user_id = $this->request->user_info->user_id;
            $UserNotesModel->staff_id = $staff_id;
        }
        $UserNotesModel->user_notes = $user_notes;
        if($UserNotesModel->save()){
            return success('操作成功');
        }else{
            return error('操作失败');
        }
    }
}